import { useCallback } from 'react';
import type { TypedKey } from '../context/types';
import { globalContext } from '../globalContext';

export function useBatch() {
  const batchUpdate = useCallback(
    (updates: Array<[TypedKey<unknown>, unknown]>) => {
      globalContext.batch(() => {
        updates.forEach(([key, value]) => {
          globalContext.setData(key, value, false);
        });
      });
    },
    [],
  );

  return batchUpdate;
}
